module namespace resultview = "http://www.xmlproject.com/smi/lib/view/resultview";

import module namespace http = "http://www.28msec.com/modules/http";
import module namespace xqddf = "http://www.zorba-xquery.com/modules/xqddf";

import module namespace utilities = "http://www.xmlproject.com/smi/lib/utilities";
declare collection resultview:collection as node()*;
declare variable $resultview:collection as xs:QName := xs:QName("resultview:collection");

declare sequential function resultview:add($allMovies as element(movies))
{
for $movie in $allMovies
    return xqddf:insert-nodes(xs:QName("resultview:collection"), $movie);
    exit returning resultview:list();
};

declare sequential function resultview:deleteList() {
  xqddf:delete-nodes(
    $resultview:collection,
    xqddf:collection($resultview:collection));
  resultview:list();
};

declare sequential function resultview:list() {
  http:set-header("Cache-Control", "no-cache");
};

declare sequential function resultview:show($imdbResults as element(searchresult),$themoviedbResults as element(searchresult), $rTomatoesResults as element(searchresult), $coo) as element()
{
let $numberofresults := 100
    return
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
            <meta name="description" content="" />
            <meta name="keywords" content="" />
            <title>Movie Search</title>
            <link href="/css/style.css" rel="stylesheet" type="text/css" />
        </head>
        <body>
            <div id="wrap">
                <div id="header" align="left">
                </div>
                
                <div id="menu">
                    <a href="index.xq" class="selected">Home Page</a>
                    <a href="aboutus.xq" class="selected">About us</a>
                </div>
                
                <div id="body">
                    <div id="top" align="center">
                        <h1>Movie Search</h1>
                        <form id="search-form" action="/moviesearch/search" method="POST">
                                <p>
                                    <input type="text" size="60" name="query" value="" />
                                    {utilities:generate-dates()}
                                    <input type="submit" value="Movie Search" />
                                </p>
                            </form>
                        <div id="results"></div>          
                        <hr/>
                    </div>
                     <div id="leftbar">
                         <span>Recently searched</span>
                            {
                            let $cookieresult := resultview:renderCookieResult($coo)
                            return $cookieresult
                            }
                    </div>
                    <div id="singleContent">
                        <center><h2>Movie Results!</h2></center>
                                {
                                    let $movie := resultview:renderSearchResult($imdbResults,$themoviedbResults, $rTomatoesResults, $numberofresults)
                                    return $movie
                                } 
                    </div>
                    <div id="footer"> Project of XML and Databases :: Master in Applied Computer Science, University of Freiburg 2012 </div>
                </div>                
                <div id="bottom">
                </div>
            </div>
        </body>
        </html>    
};

declare sequential function resultview:renderCookieResult($coo)
{
    let $items := 
        for $i in $coo//search
        return <li><a href="/moviesearch/search?query={$i/text()}">{$i/text()}</a></li>
    
    return <ul>{$items}</ul>
    
};

declare sequential function resultview:moviesOrder()
{
let $a := ""
return 
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
            <meta name="description" content="" />
            <meta name="keywords" content="" />
            <title>Movie Search</title>
            <link href="/css/style.css" rel="stylesheet" type="text/css" />
            <title>Movie Search</title>
            </head>
        <body>
            <div id="wrap">
                <div id="header" align="left">
                </div>
                
                <div id="menu">
                    <a href="index.xq" class="selected">Home Page</a>
                    <a href="aboutus.xq" class="selected">About us</a>
                </div>
                
                <div id="body">
                    <div id="top" align="center">
                        <h1>Movie Search</h1>
                        <form id="search-form" action="/moviesearch/search" method="POST">
                                <p>
                                    <input type="text" size="60" name="query" value="" />
                                    {utilities:generate-dates()}
                                    <input type="submit" value="Movie Search" />
                                </p>
                            </form>
                        <div id="results"></div>          
                        <hr/>
                    </div>
                    <div id="singleContent">
                        <center><h2>Movie Results!</h2></center>
                        {
                        let $movie := resultview:renderSearchResultTitle()
                        return $movie
                        } 
                    </div>
                    <div id="footer"> Project of XML and Databases :: Master in Applied Computer Science, University of Freiburg 2012 </div>
                </div>
                
                <div id="bottom">
                </div>
            </div>
        </body>
        </html>  
         
};
declare sequential function resultview:moviesOrderYear()
{
let $a := ""
return 
<html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
            <meta name="description" content="" />
            <meta name="keywords" content="" />
            <title>Movie Search</title>
            <link href="/css/style.css" rel="stylesheet" type="text/css" />
            <title>Movie Search</title>
            </head>
        <body>
            <div id="wrap">
                <div id="header" align="left">
                </div>
                
                <div id="menu">
                    <a href="index.xq" class="selected">Home Page</a>
                    <a href="aboutus.xq" class="selected">About us</a>
                </div>
                
                <div id="body">
                    <div id="top" align="center">
                        <h1>Movie Search</h1>
                        <form id="search-form" action="/moviesearch/search" method="POST">
                                <p>
                                    <input type="text" size="60" name="query" value="" />
                                    {utilities:generate-dates()}
                                    <input type="submit" value="Movie Search" />
                                </p>
                            </form>
                        <div id="results"></div>          
                        <hr/>
                    </div>
                    <div id="singleContent">
                        <center><h2>Movie Results!</h2></center>
                        {
                        let $movie := resultview:renderSearchResultYear()
                        return $movie
                        } 
                    </div>
                    <div id="footer"> Project of XML and Databases :: Master in Applied Computer Science, University of Freiburg 2012 </div>
                </div>
                
                <div id="bottom">
                </div>
            </div>
        </body>
        </html>  
         
};

declare sequential function resultview:renderSearchResultTitle()
{
    let $a := ""
    return 
        <table border="0">
            <tr>
                <td><center><h3>Check movie!</h3></center></td>
                <td><center><h3><a href="/movieorder/showmovieyear">Year</a></h3></center></td>
                <td><center><h3><a href="/movieorder/showmovie">Title</a></h3></center></td>
                <td><center><h3>Sources</h3></center></td>
            </tr>
            {       
                for $movie in xqddf:collection($resultview:collection)[1]/movie
                order by $movie/title
                let $title := data($movie/title)
                let $plot := data($movie/plot)
                let $imdbid := data($movie/sourcesAvailable/imdb/id)
                let $director := data($movie/sourcesAvailable/imdb/director)
                let $genre := data($movie/sourcesAvailable/imdb/genre)
                let $tmdbid := data($movie/sourcesAvailable/tmdb/id)
                let $tmdbRating := data($movie/sourcesAvailable/tmdb/rating)
                let $rtid := data($movie/sourcesAvailable/rottenTomatoes[1]/id)
                let $rtRating := data($movie/sourcesAvailable/rottenTomatoes[1]/rating)
                let $year := data($movie/year)
                
                (: get rid of all line feeds and carriage returns :)
                let $plot := normalize-space($movie/plot/text()) 
                return 
                    <tr>
                        <td valign="top">
                            <center>
                            <a href="/moviesinfo/showmovie?title={$title}&amp;imdbid={$imdbid}&amp;tmdbid={$tmdbid}&amp;rtid={$rtid}"><img src="/images/CHECK_Movie.png" width="100" height="50"/></a>
                            </center>
                        </td>
                        <td valign="top">
                            <p>{$year}</p>
                        </td>
                        <td valign="top" width="400">
                            <h4><p><a href="/moviesinfo/showmovie?title={$title}&amp;imdbid={$imdbid}&amp;tmdbid={$tmdbid}&amp;rtid={$rtid}">{$title}</a></p></h4>
                            {
                                if (string-length($director)>0)
                                then <p>Director: {$director}</p>
                                else()
                            }
                            {
                                if (string-length($genre)>0)
                                then <p>Genre: {$genre}</p>
                                else()
                            }
                            <p>Rating: {$tmdbRating} 
                             {
                                if (string-length($rtRating)>0)
                                then concat(" - ", $rtRating)
                                else()
                            }                        
                            </p>
                            <p>{$plot}</p>
                        </td>
                        <td valign="top">
                            <center>
                                {
                                   if(string-length($imdbid) > 0)
                                   then <img src="/images/imdb_logo.png" width="100" height="50"/>                      
                                   else()
                                }
                                {
                                   if(string-length($tmdbid) > 0)
                                   then <img src="/images/tmdb_logo.png" width="100" height="50"/>
                                   else()
                                }
                                {
                                   if(string-length($rtid) > 0)
                                   then <img src="/images/rt_logo.png" width="100" height="50"/>
                                   else()
                                }
                                </center>
                        </td>
                    </tr>
            } 
        </table>
};



declare sequential function resultview:renderSearchResultYear()
{
    let $a := ""
    return 
        <table border="0">
            <tr>
                <td><center><h3>Check movie!</h3></center></td>
                <td><center><h3><a href="/movieorder/showmovieyear">Year</a></h3></center></td>
                <td><center><h3><a href="/movieorder/showmovie">Title</a></h3></center></td>
                <td><center><h3>Sources</h3></center></td>
            </tr>
            {       
                for $movie in xqddf:collection($resultview:collection)[1]/movie
                order by $movie/year
                (:let $a := resultview:deleteList():)
                let $title := data($movie/title)
                let $plot := data($movie/plot)
                let $imdbid := data($movie/sourcesAvailable/imdb/id)
                let $director := data($movie/sourcesAvailable/imdb/director)
                let $genre := data($movie/sourcesAvailable/imdb/genre)
                let $tmdbid := data($movie/sourcesAvailable/tmdb/id)
                let $tmdbRating := data($movie/sourcesAvailable/tmdb/rating)
                let $rtid := data($movie/sourcesAvailable/rottenTomatoes[1]/id)
                let $rtRating := data($movie/sourcesAvailable/rottenTomatoes[1]/rating)
                let $year := data($movie/year)
                
                (: get rid of all line feeds and carriage returns :)
                let $plot := normalize-space($movie/plot/text()) 
                return 
                    <tr>
                        <td valign="top">
                            <center>
                            <a href="/moviesinfo/showmovie?title={$title}&amp;imdbid={$imdbid}&amp;tmdbid={$tmdbid}&amp;rtid={$rtid}"><img src="/images/CHECK_Movie.png" width="100" height="50"/></a>
                            </center>
                        </td>
                        <td valign="top">
                            <p>{$year}</p>
                        </td>
                        <td valign="top" width="400">
                            <h4><p><a href="/moviesinfo/showmovie?title={$title}&amp;imdbid={$imdbid}&amp;tmdbid={$tmdbid}&amp;rtid={$rtid}">{$title}</a></p></h4>
                            {
                                if (string-length($director)>0)
                                then <p>Director: {$director}</p>
                                else()
                            }
                            {
                                if (string-length($genre)>0)
                                then <p>Genre: {$genre}</p>
                                else()
                            }
                            <p>Rating: {$tmdbRating} 
                             {
                                if (string-length($rtRating)>0)
                                then concat(" - ", $rtRating)
                                else()
                            }                        
                            </p>
                            <p>{$plot}</p>
                        </td>
                        <td valign="top">
                            <center>
                                {
                                   if(string-length($imdbid) > 0)
                                   then <img src="/images/imdb_logo.png" width="100" height="50"/>                      
                                   else()
                                }
                                {
                                   if(string-length($tmdbid) > 0)
                                   then <img src="/images/tmdb_logo.png" width="100" height="50"/>
                                   else()
                                }
                                {
                                   if(string-length($rtid) > 0)
                                   then <img src="/images/rt_logo.png" width="100" height="50"/>
                                   else()
                                }
                                </center>
                        </td>
                    </tr>
            } 
        </table>
};

declare sequential function resultview:renderSearchResult($imdbResults as element(searchresult), $themoviedbResults as element(searchresult), $rTomatoesResults as element(searchresult), $numberofresults as xs:integer) as element()
{
    let $a := resultview:deleteList()
    let $allResources := resultview:combineResponses($imdbResults, $themoviedbResults, $rTomatoesResults)
    
    return
        <table border="0">
            <tr>
                <td><center><h3>Check movie!</h3></center></td>
                <td><center><h3><a href="/movieorder/showmovieyear">Year</a></h3></center></td>
                <td><center><h3><a href="/movieorder/showmovie">Title</a></h3></center></td>
                <td><center><h3>Sources</h3></center></td>
            </tr>
            {   
                for $movie in $allResources/movie[position() < $numberofresults]
                let $title := data($movie/title)
                let $plot := data($movie/plot)
                let $imdbid := data($movie/sourcesAvailable/imdb/id)
                let $director := data($movie/sourcesAvailable/imdb/director)
                let $genre := data($movie/sourcesAvailable/imdb/genre)
                let $tmdbid := data($movie/sourcesAvailable/tmdb/id)
                let $tmdbRating := data($movie/sourcesAvailable/tmdb/rating)
                let $rtid := data($movie/sourcesAvailable/rottenTomatoes[1]/id)
                let $rtRating := data($movie/sourcesAvailable/rottenTomatoes[1]/rating)
                let $year := data($movie/year)
                
                (: get rid of all line feeds and carriage returns :)
                let $plot := normalize-space($movie/plot/text()) 
                return 
                    <tr>
                        <td valign="top">
                            <center>
                            <a href="/moviesinfo/showmovie?title={$title}&amp;imdbid={$imdbid}&amp;tmdbid={$tmdbid}&amp;rtid={$rtid}"><img src="/images/CHECK_Movie.png" width="100" height="50"/></a>
                            </center>
                        </td>
                        <td valign="top">
                            <p>{$year}</p>
                        </td>
                        <td valign="top" width="400">
                            <h4><p><a href="/moviesinfo/showmovie?title={$title}&amp;imdbid={$imdbid}&amp;tmdbid={$tmdbid}&amp;rtid={$rtid}">{$title}</a></p></h4>
                            {
                                if (string-length($director)>0)
                                then <p>Director: {$director}</p>
                                else()
                            }
                            {
                                if (string-length($genre)>0)
                                then <p>Genre: {$genre}</p>
                                else()
                            }
                            <p>Rating: {$tmdbRating} 
                             {
                                if (string-length($rtRating)>0)
                                    then concat(" - ", $rtRating)
                                else()
                            }                        
                            </p>
                            <p>{$plot}</p>
                        </td>
                        <td valign="top">
                            <center>
                                {
                                   if(string-length($imdbid) > 0)
                                   then <img src="/images/imdb_logo.png" width="100" height="50"/>                      
                                   else()
                                }
                                {
                                   if(string-length($tmdbid) > 0)
                                   then <img src="/images/tmdb_logo.png" width="100" height="50"/>
                                   else()
                                }
                                {
                                   if(string-length($rtid) > 0)
                                   then <img src="/images/rt_logo.png" width="100" height="50"/>
                                   else()
                                }
                                </center>
                        </td>
                    </tr>
            } 
        </table>
};


declare sequential function resultview:combineResponses($imdbResults as element(searchresult), $themoviedbResults as element(searchresult), $rTomatoesResults as element(searchresult)) as element()
{
    let $allResults :=
    <movies>   
        {
        for $movie in $themoviedbResults//movie
        return
            <movie>
                <title>{$movie/title}</title>
                <year>{$movie/year}</year>
                <sourcesAvailable>
                    <tmdb>
                        <id>{string($movie/@tmdbid)}</id>
                        <rating>{string($movie/specialinfo/rating)}</rating>
                    </tmdb>
                    {
                        for $imdbMovie in $imdbResults//movie
                        return
                            if(
                                $imdbMovie/year = $movie/year
                                and
                                utilities:similarTitles($movie/title, $imdbMovie/title)                            
                            )
                            then 
                                <imdb>
                                    <id>{string($imdbMovie/@imdbid)}</id>
                                    <director>{string($imdbMovie/specialinfo/director)}</director>
                                    <genre>{string($imdbMovie/specialinfo/genre)}</genre>
                                </imdb>
                            else if (string-length($movie/@imdbid)>0)
                            then 
                                <imdb>
                                    <id>{string($movie/@imdbid)}</id>
                                </imdb>                            
                            else()
                     }
                     {
                        for $rtMovie in $rTomatoesResults//movie
                        return
                            if(
                                $rtMovie/year = $movie/year
                                and
                                utilities:similarTitles($movie/title, $rtMovie/title)                               
                            )
                            then
                                <rottenTomatoes>
                                    <id>{string($rtMovie/@rtid)}</id>
                                    <rating>{string($rtMovie/specialinfo/rating)}</rating>
                                </rottenTomatoes>
                            else()
                     }
                </sourcesAvailable>
            </movie>
        }
    </movies>
    let $a := resultview:add($allResults)
    return $allResults
};
